package com.bysj.lms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bysj.lms.mapper.UserMapper;
import com.bysj.lms.pojo.entity.LmsBr;
import com.bysj.lms.mapper.LmsBrMapper;
import com.bysj.lms.pojo.dto.LmsBrDto;
import com.bysj.lms.service.ILmsBrService;
import com.bysj.lms.utils.MailUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaojiakai
 * @since 2022-02-04
 */
@Slf4j
@Service
public class LmsBrServiceImpl extends ServiceImpl<LmsBrMapper, LmsBr> implements ILmsBrService {

    @Autowired
    LmsBrMapper lmsBrMapper;
    @Autowired
    LmsFaServiceImpl lmsFaService;
    @Autowired
    LmsLveServiceImpl lmsLveService;

    @Override
    public PageInfo<LmsBrDto> findByPage(String name,String uid,String status,Integer page, Integer count) {

        PageHelper.startPage(page,count);
        List<LmsBrDto> lmsBrDtos = lmsBrMapper.find(name,uid,status);
        return new PageInfo(lmsBrDtos);
    }

    @Override
    public boolean approve(LmsBr lmsBr) {
        String status = lmsBr.getStatus();
        String type = lmsBr.getType();
        String instrumentId = lmsBr.getInstrumentId();
        boolean update = lambdaUpdate().eq(LmsBr::getId, lmsBr.getId())
                .set(LmsBr::getHandleTime, LocalDateTime.now())
                .set(LmsBr::getStatus,status)
                .update();

        if(status.equals("1")){
            //固定资产
            if(type.equals("0"))
                lmsFaService.changeStatus("1",instrumentId);
            //低值
            else if(type.equals("1"))
                lmsLveService.changeStatus("1",instrumentId);
        }
        return update;
    }

    @Override
    public boolean giveBack(String id) {
        boolean giveBack = lambdaUpdate().eq(LmsBr::getId, id).set(LmsBr::getReturnTime, LocalDateTime.now())
                .set(LmsBr::getStatus,"3").update();
        return giveBack;
    }

    @Override
    public boolean approveBatch(List<LmsBr>lmsBrs) {
        boolean b = true;
        for (LmsBr lmsbr:lmsBrs) {
            b = approve(lmsbr);
        }
        return b;
    }

    @Scheduled(cron = "0 0/30 * * * ?")
    @Override
    public void TimeOutListener() {
        log.info("超时定时任务=============");

        List<LmsBrDto> lmsBrs = lmsBrMapper.TimeOutList();
        if(lmsBrs.size() != 0){
            String[] ids = lmsBrs.stream().map(LmsBr::getId).toArray(String[]::new);
            lmsBrMapper.statusBatch(ids,"4");
            lmsBrs.forEach(lmsBr -> {
                String subject = "实验室设备借用超时提醒";
                String toMail = lmsBr.getEmail();
                String text = "亲爱的"+lmsBr.getName()+"老师：您于"+lmsBr.getHandleTime()
                        +"借的"+lmsBr.getItemName()+"设备借用时间已超时，请于三天内归还或续借，否则将关闭您的借用权限";
                MailUtil.sendEmail(subject,toMail,text);
            });
        }

    }
    
    @Override
    public boolean applyLoan(LmsBr lmsBr) {
        boolean save = save(lmsBr);
        if(lmsBr.getType()=="0"){
            //固定
            lmsFaService.changeStatus("1",lmsBr.getInstrumentId());
        }else{
            lmsLveService.changeStatus("1",lmsBr.getInstrumentId());
        }
        return save?true:false;
    }
}
